package howl.adaptor;

import howl.Twitter.TwitterProvider;
import howl.model.Model;
import howl.types.HowlUser;

import java.util.ArrayList;
import java.util.List;

import compiler.nonterminals.CreateStmt;
import compiler.nonterminals.DeleteStmt;
import compiler.nonterminals.InsertStmt;

import twitter4j.DirectMessage;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.http.AccessToken;

public class TwitterAdaptor extends DuplexAdaptor {

	private Twitter twitter;

	public TwitterAdaptor(HowlUser u) {
		super(u);
		this.twitter = new Twitter("UoWHowl", "wolverhampton");
	}

	@Override
	public List<Command> get() {
		List<Command> c = new ArrayList<Command>();
		List<DirectMessage> l;
		try {
			l = this.twitter.getDirectMessages();
			HowlUser hu;
			for (int i = 0; i < l.size(); i++) {
				hu = Model.getHowlUserfromTwitterName(l.get(i).getSender()
						.getScreenName());

				c.add(new Command(l.get(i).getText(), hu));
				this.twitter.destroyDirectMessage(l.get(i).getId());

			}
		} catch (Exception e) {

		}
		return c;
	}

	@Override
	public boolean put(String j) {
		if(j instanceof String) {
			String str = String.class.cast(j);
		Twitter t = TwitterProvider.getTwitter();
		AccessToken a = new AccessToken(this.user.getTwitterToken(), this.user.getTwitterTokenSecret());
		Twitter u = TwitterProvider.getTwitter();
		u.setOAuthAccessToken(a);
		try {
			t.sendDirectMessage(this.user.getTwitterName(), str);
		} catch (TwitterException e) {
			e.printStackTrace();
			return false;
		}
		return true;
		}
		return false;
	}

	@Override
	public void doCreate(CreateStmt ht) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doDelete(DeleteStmt ht) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doInsert(InsertStmt ht) {
		// TODO Auto-generated method stub
		
	}

}
